"""
Case Type   : SQL语法/table
Case Name   : 分区表默认创建索引类型校验
Create At   : 2023/02/17
Owner       : cr13
Description :
    1.创建分区表
    2.不指定local/global关键字创建索引
    3.查询系统表pg_partition中索引类型
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.默认创建local索引
    4.成功
History     :
"""

import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.com = Common()
        self.commsh = CommonSH('PrimaryDbUser')
        self.tb_name = 't_ddl_index_type_0323'
        self.index = 'i_ddl_index_type_0323'

    def test_index_type(self):
        text = "--step1:创建分区表 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name} cascade;" \
            f"create table {self.tb_name}(c1 int,c2 int) " \
            f"partition by range(c1)" \
            f"(" \
            f"  partition p1 values less than (10)," \
            f"  partition p2 values less than (20)," \
            f"  partition p3 values less than (maxvalue));"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, result, '执行失败'
                      + text)

        text = "--step2:不指定local/global关键字创建索引 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop index if exists {self.index};" \
            f"create unique index {self.index} on {self.tb_name} using " \
            f"btree(c1);"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_INDEX_SUCCESS_MSG, result, '执行失败'
                      + text)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, result,
                      '执行失败' + text)

        text = "--step3:查询系统表pg_partition中索引类型 " \
               "expect:默认创建local索引--"
        self.log.info(text)
        name = []
        sql_cmd = f"select relname from pg_partition order by relname;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        name.append(str(result.splitlines()[-4]).strip())
        name.append(str(result.splitlines()[-6]).strip())
        name.append(str(result.splitlines()[-8]).strip())
        for i in name:
            sql_cmd = f"select parttype from pg_partition where relname = " \
                f"'{i}';"
            result = self.commsh.execut_db_sql(sql_cmd)
            self.log.info(result)
            self.assertEqual(str(result.splitlines()[-2]).strip(), 'p',
                             '执行失败' + text)

    def tearDown(self):
        text = "--step4:清理环境 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name} cascade;"
        drop_result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(drop_result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_result, '执行失败'
                      + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
